﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;


public delegate void MiEventoEventHandler(string param1, string param2);

public partial class Controles_Browser : System.Web.UI.UserControl
{
    
    public event MiEventoEventHandler MiEvento;    

    private List<string> Fields = new List<string>();

    public List<string> Campos
    {
        get { return Fields; }
        set { Fields = value; }
    }
    public void setCampos(){
        int iPos = 0;
        foreach (string Nombre in Campos)
        {
            
            
            BoundField Col = new BoundField();
            Col.DataField = Nombre;
            Col.HeaderText = Nombre;
            Col.ControlStyle.Width = 200;
            grvBrow.Columns.Add(Col);
            if (iPos == 0)
            {
                string[] key= { Nombre};

                grvBrow.DataKeyNames = key;
                Col.Visible = false;
            }
            iPos++;

            
        }
        ButtonField btn = new ButtonField();
        btn.CommandName = "Select";
        btn.Text = "Ver";
        btn.ButtonType = ButtonType.Link;
        grvBrow.Columns.Add(btn);

         btn = new ButtonField();
        btn.CommandName = "Delete";
        btn.Text = "Eliminar";
        btn.ButtonType = ButtonType.Link;
        grvBrow.Columns.Add(btn);

    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private DataTable dsSource;
    public DataTable DsSource
    {
        get { return dsSource; }
        set { dsSource = value; }
    }
    
    
    
    public void Refresh(){
        grvBrow.AllowPaging = true;
        grvBrow.DataSource = dsSource;
        grvBrow.AutoGenerateColumns = false;

        
        grvBrow.DataBind();
    }


    protected void grvBrow_PageIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void grvBrow_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvBrow.PageIndex=e.NewPageIndex;
        grvBrow.DataBind();
    }
    protected void grvBrow_SelectedIndexChanged(object sender, EventArgs e)
    {
       if (MiEvento != null)
        {
            // El evento se lanza como cualquier delegado
            MiEvento.Invoke("Selecciono", grvBrow.SelectedDataKey.Value.ToString());
        }


    }
    protected void grvBrow_Sorted(object sender, EventArgs e)
    {
        
       
    }
    protected void grvBrow_Sorting(object sender, GridViewSortEventArgs e)
    {

    }
    protected void grvBrow_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (MiEvento != null)
        {
            // El evento se lanza como cualquier delegado
            MiEvento.Invoke("Borrar", grvBrow.DataKeys[e.RowIndex].Value.ToString());
        }
    }
}
